/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.zhixun.mobile.modules.wechat.web;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.dao.UserDao;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.momarketing.entity.CompanyNews;
import com.boxin.ims.modules.momarketing.entity.MicroPage;
import com.boxin.ims.modules.momarketing.entity.OnlineSignup;
import com.boxin.ims.modules.momarketing.entity.OnlineSignupConfig;
import com.boxin.ims.modules.momarketing.entity.PageTemplate;
import com.boxin.ims.modules.momarketing.entity.Project;
import com.boxin.ims.modules.momarketing.entity.self.OnlineSignupConfigDetail;
import com.boxin.ims.modules.momarketing.entity.self.OnlineSignupDetail;
import com.boxin.ims.modules.momarketing.service.CompanyNewsService;
import com.boxin.ims.modules.momarketing.service.CustomPageService;
import com.boxin.ims.modules.momarketing.service.MicroPageService;
import com.boxin.ims.modules.momarketing.service.MicroProductService;
import com.boxin.ims.modules.momarketing.service.OnlineSignupConfigService;
import com.boxin.ims.modules.momarketing.service.OnlineSignupService;
import com.boxin.ims.modules.momarketing.service.ProjectMenuService;
import com.boxin.ims.modules.momarketing.service.ProjectService;
import com.boxin.ims.modules.momarketing.service.ProjectVisitService;
import com.boxin.ims.modules.wechat.entity.car.CarOnlineSignup;
import com.boxin.ims.modules.wechat.entity.car.CarOnlineSignupConfig;
import com.boxin.ims.modules.wechat.entity.car.CarOnlineUpkeep;
import com.boxin.ims.modules.wechat.entity.car.CarType;
import com.boxin.ims.modules.wechat.service.WechatDaoHelper;
import com.boxin.ims.modules.wechat.service.car.CarOnlineSignupConfigService;
import com.boxin.ims.modules.wechat.service.car.CarOnlineSignupService;
import com.boxin.ims.modules.wechat.service.car.CarOnlineUpkeepService;
import com.boxin.ims.modules.wechat.service.car.CarTypeService;
import com.boxin.ims.modules.wechat.utils.AssertUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 在线预约Controller
 * 
 * @author Jakemanse
 * @version 2013-09-10
 */
@Controller
@RequestMapping(value ="/CarOnlineSignUp")
public class CarOnlineSignupFrontController extends BaseController {

	
	@Autowired
	private CarOnlineUpkeepService carOnlineUpkeepService;
	

	@Autowired
	SystemService systemService;

	@Autowired
	OnlineSignupConfigService onlineSignupConfigService;

	@Autowired
	WechatDaoHelper wechatDaoHelper;
	
	@Autowired
	private ProjectService projectService;
	
	@Autowired
	private MicroPageService microPageService;
	
	@Autowired
	private CarOnlineSignupService carOnlineSignupService;
	
	@PersistenceContext
	private EntityManager entityManager;
	
	@Autowired
	private CarTypeService carTypeService;
	
	@Autowired
	private CarOnlineSignupConfigService carOnlineSignupConfigService;

	@RequestMapping(value = "toCarOnlineSignUp/{proj_id}")
	public String list(@PathVariable Long proj_id,OnlineSignup onlineSignup, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		/* 清除二级缓存 */
		EntityManagerFactory entityManagerFactory = entityManager.getEntityManagerFactory();
		/* 清除实体缓存 */
		entityManagerFactory.getCache().evict(Project.class);
		entityManagerFactory.getCache().evict(CompanyNews.class);
		entityManagerFactory.getCache().evict(MicroPage.class);
//	}
	
	/* 获取页面要展现的资源 */
	/* - 菜单展现公共项 */
	Project project = projectService.get(proj_id);
	/* -- 网站主题 */
	String theme = getProjectTheme(project);
	request.setAttribute("theme", theme);
	request.setAttribute("project", project);
	
	
	String memberId=request.getParameter("memberId");
	String wechatId=request.getParameter("wechatId");
	
	model.addAttribute("wechatId", wechatId);
	model.addAttribute("memberId", memberId);
	
	CarOnlineSignupConfig carOnlineSignupConfig=new CarOnlineSignupConfig();
	carOnlineSignupConfig.setUser(project.getUser());
	carOnlineSignupConfig.setType("0");
	List<CarOnlineSignupConfig> carOnlineSignupConfigList= carOnlineSignupConfigService.findCarOnlineSignupConfigByUserId(carOnlineSignupConfig);
	
	if(carOnlineSignupConfigList.size()>0){
		
		CarType carType2=new CarType();
		carType2.setUser(project.getUser());
		carType2.setParent(carOnlineSignupConfigList.get(0).getCarType());
		List<CarType> carTypeList= carTypeService.findByUserId(carType2);
		
		request.setAttribute("carTypeList", carTypeList);
	}
	
	
	List<MicroPage> microPages= microPageService.findMicropagesByProjectId(proj_id);
	request.setAttribute("microPages", microPages);
		return "modules/site/themes/QiCheFuWu/yuyueshijia";
	}
	
	
	@RequestMapping(value = "toCarOnlineSignUpKeep/{proj_id}")
	public String toCarOnlineSignUpKeep(@PathVariable Long proj_id,OnlineSignup onlineSignup, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		/* 清除二级缓存 */
		EntityManagerFactory entityManagerFactory = entityManager.getEntityManagerFactory();
		/* 清除实体缓存 */
		entityManagerFactory.getCache().evict(Project.class);
		entityManagerFactory.getCache().evict(CompanyNews.class);
		entityManagerFactory.getCache().evict(MicroPage.class);
//	}
	
	/* 获取页面要展现的资源 */
	/* - 菜单展现公共项 */
	Project project = projectService.get(proj_id);
	/* -- 网站主题 */
	String theme = getProjectTheme(project);
	request.setAttribute("theme", theme);
	request.setAttribute("project", project);
	
	
	String memberId=request.getParameter("memberId");
	String wechatId=request.getParameter("wechatId");
	
	model.addAttribute("wechatId", wechatId);
	model.addAttribute("memberId", memberId);
	
	CarOnlineSignupConfig carOnlineSignupConfig=new CarOnlineSignupConfig();
	carOnlineSignupConfig.setUser(project.getUser());
	carOnlineSignupConfig.setType("1");
	List<CarOnlineSignupConfig> carOnlineSignupConfigList= carOnlineSignupConfigService.findCarOnlineSignupConfigByUserId(carOnlineSignupConfig);
	
	if(carOnlineSignupConfigList.size()>0){
		
		CarType carType2=new CarType();
		carType2.setUser(project.getUser());
		carType2.setParent(carOnlineSignupConfigList.get(0).getCarType());
		List<CarType> carTypeList= carTypeService.findByUserId(carType2);
		
		request.setAttribute("carTypeList", carTypeList);
	}
	
	
	List<MicroPage> microPages= microPageService.findMicropagesByProjectId(proj_id);
	request.setAttribute("microPages", microPages);
		return "modules/site/themes/QiCheFuWu/yuyuebaoyang";
	}

	@RequestMapping(value = { "reg/{uid}", "" })
	public String form(@PathVariable Long uid, OnlineSignup onlineSignup,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Long cid = null;
		User user = systemService.getUser(uid);
		String id = request.getParameter("cid");
		if (AssertUtils.notNullOrEmpty(id)) {
			cid = Long.parseLong(id);
		}
		OnlineSignupConfig config = onlineSignupConfigService.getByUserId(uid,cid);
		if (config != null) {
			onlineSignup.setStartTime(config.getStartTime());
			onlineSignup.setEndTime(config.getEndTime());
			String selfDefin = config.getSelfDefine();

			if (AssertUtils.notNullOrEmpty(selfDefin)) {
				Map<String, Object> map = new HashMap<String, Object>();

				map.put("idList", selfDefin);

				config.setOnlineSignupConfigDetailsList(wechatDaoHelper
						.selectSignupConfigDetailByIDList(map));
			} else {
				config.setOnlineSignupConfigDetailsList(null);
			}

		}
		onlineSignup.setUser(user);
		model.addAttribute("onlineSignup", onlineSignup);
		model.addAttribute("config", config);
		model.addAttribute("uid", uid);
		model.addAttribute("cid", cid);

		//
		return "modules/ims/front/mobile/signup";
	}

	@RequiresPermissions("wechat:car:carOnlineSignup:edit")
	@RequestMapping(value = "save")
	public String save(HttpServletRequest request,CarOnlineSignup carOnlineSignup, Model model, RedirectAttributes redirectAttributes) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		
		String signupTime=request.getParameter("signupTime");
		carOnlineSignup.setSignupTime(sdf.parse(signupTime));
		carOnlineSignupService.save(carOnlineSignup);
		String proj_id=request.getParameter("proj_id");
		return "redirect:/site/"+proj_id;
	}
	
	@RequestMapping(value = "saveUpKeep")
	public String save(HttpServletRequest request,CarOnlineUpkeep carOnlineUpkeep, Model model, RedirectAttributes redirectAttributes) {
		carOnlineUpkeepService.save(carOnlineUpkeep);
		String proj_id=request.getParameter("proj_id");
		return "redirect:/site/"+proj_id;
	}
	
	public String getProjectTheme(Project project){
		PageTemplate template = project.getPageTemplate();
		
		/* 获取主题样式 */
		String theme = template.getTheme();
		if(theme == null || "".equals(theme)){
			theme = "default";
		}
		return theme;
	}
	
	
	@RequestMapping(value = "getCarTypeByParentId")
	public void getCarTypeByParentId(HttpServletRequest request,HttpServletResponse response, CarOnlineUpkeep carOnlineUpkeep, Model model, RedirectAttributes redirectAttributes) {
		String parentTypeId=request.getParameter("parentTypeId");
		CarType carType=new CarType();
		CarType parentCarType=new CarType(Long.parseLong(parentTypeId));
		carType.setParent(parentCarType);
		List<CarType> carTypeList= carTypeService.findByUserId(carType);
		List<Map<String, Object>> mapList = Lists.newArrayList();
		for (int i = 0; i < carTypeList.size(); i++) {
			Map<String, Object> map= Maps.newHashMap();
			map.put("name", carTypeList.get(i).getName());
			map.put("id", carTypeList.get(i).getId());
			mapList.add(map);
		}
		writeJson(request, response, mapList);
	}
	

}
